package com.kgcx.flight.service.Impl;

import com.kgcx.common.bean.ReturnData;
import com.kgcx.common.constant.ResultCode;
import com.kgcx.common.utils.http.HttpUtils;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.flight.service.IFlightService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.SortedMap;
import java.util.TreeMap;

@Service
public class VariFlightService implements IFlightService {

    private static final Logger LOG = LoggerFactory.getLogger(VariFlightService.class);

    @Value("${vari_flight_api_name}")
    private String vari_flight_api_name;
    @Value("${vari_flight_api_flight}")
    private String vari_flight_api_flight;
    @Value("${vari_flight_api_addflightpush}")
    private String vari_flight_api_addflightpush;
    @Value("${vari_flight_app_id}")
    private String vari_flight_app_id;
    @Value("${vari_flight_app_secret}")
    private String vari_flight_app_secret;

    @Override
    public ReturnData<String> QueryFlight(String fnum, String date)
    {
        ReturnData<String> rtn = new ReturnData<>();
        ////        1.先从redis中取
//        Object obj = stringRedisTemplate.opsForHash()
//                .get(OrderConstant.ORDERS_ING, order_no);
//        if(obj != null){
//            itm = JsonUtil.toObject(obj.toString(), Order.class);
//        }else {
//            //2. redis中没有的话，从数据库取
//            itm = service.getByOrderNo(requestData.getString("order_no"));
//        }
//        return new ReturnData(itm);
        SortedMap<String, Object> params = new TreeMap<String, Object>();
        params.put("appid", vari_flight_app_id);
        params.put("fnum", fnum);
        params.put("date", date);
        for (String key : params.keySet()) {
            System.out.println(key + " : " + params.get(key));
        }
        String token = HelpUtil.MD5(HelpUtil.MD5(HelpUtil.implode("&", params) + vari_flight_app_secret));

        String url = String.format(vari_flight_api_flight + HelpUtil.implode("&", params) + "&token=" + token);
        System.out.println("请求的URL：" + url);
        try {
            String result = HttpUtils.getInstance().syncGetRequest(url);
            rtn.setCode(ResultCode.SUCCESS);
            rtn.setMsg(String.format("{%s}-航班查询成功",vari_flight_api_name));
            rtn.setData(HelpUtil.unicodeToString(result));
//            JsonUtil.toObject(result, List<FlightInfo>.getClass())
        } catch (Exception ex) {
            LOG.error("VariFlightService.queryFlight ex", ex);
            rtn.setCode(ResultCode.FAIL);
            rtn.setMsg(String.format("{%s}-航班查询异常",vari_flight_api_name));
        }
        return rtn;
    }

    public ReturnData<String> Booking(String fnum, String date, String dep, String arr)
    {
        ReturnData<String> rtn = new ReturnData<>();
        SortedMap<String, Object> params = new TreeMap<String, Object>();
        params.put("appid", vari_flight_app_id);
        params.put("date", date);
        params.put("fnum", fnum);
        params.put("dep", dep);
        params.put("arr", arr);
        for (String key : params.keySet()) {
            System.out.println(key + " : " + params.get(key));
        }
        String token = HelpUtil.MD5(HelpUtil.MD5(HelpUtil.implode("&", params) + vari_flight_app_secret));

        String url = String.format(vari_flight_api_addflightpush + HelpUtil.implode("&", params) + "&token=" + token);
        System.out.println("请求的URL：" + url);
        try {
            String result = HttpUtils.getInstance().syncGetRequest(url);
            rtn.setCode(ResultCode.SUCCESS);
            rtn.setMsg(String.format("{%s}-订制航班推送成功",vari_flight_api_name));
            rtn.setData(HelpUtil.unicodeToString(result));
        } catch (Exception ex) {
            rtn.setCode(ResultCode.FAIL);
            rtn.setMsg(String.format("{%s}-订制航班推送异常",vari_flight_api_name));
        }
        return rtn;
    }

    public ReturnData<String> Listener(String content)
    {
        ReturnData<String> rtn = new ReturnData<>();
        LOG.info(String.format("飞常准推送过来的信息content：{%s}",HelpUtil.unicodeToString(content)));
        rtn.setCode(ResultCode.SUCCESS);
        rtn.setMsg("收到推送消息");
        rtn.setData(HelpUtil.unicodeToString(content));
        return rtn;
    }

}
